/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.entities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.util.SubjectComparator;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-28
 * 
 */
public class ISentence implements Comparable<ISentence>{
	private HashSet<Triple> triples = new HashSet<Triple>();
	private HashMap<Integer, ArrayList<byte[]>> doc_sents = new HashMap<Integer, ArrayList<byte[]>>();
	private String leadingPoint;

	public ISentence(String leadingPoint) {
		this.leadingPoint = leadingPoint;
	}

	public void addSent(int doc_id, byte[] sentence_hash) {
		String tmpSubject = null;
		if (doc_sents.containsKey(doc_id)) {
			doc_sents.get(doc_id).add(sentence_hash);
		} else {
			ArrayList<byte[]> sent = new ArrayList<byte[]>();
			sent.add(sentence_hash);
			doc_sents.put(doc_id, sent);
		}
		try {
			Connection conn = DBConnectionFactory.getConnection();//.getFalconConnection();
//			PreparedStatement ps = conn
//					.prepareStatement("SELECT s, p, o FROM quadruple where c = ? and sentence_hash = ?");
			PreparedStatement ps = conn
					.prepareStatement("SELECT s, p, o FROM quadruple where doc_id = ? and sentence_hash = ?");
			ps.setInt(1, doc_id);
			ps.setBytes(2, sentence_hash);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				String s = rs.getString(1);
				String p = rs.getString(2);
				String o = rs.getString(3);
				Triple t = new Triple(s, p, o);
				triples.add(t);
				if (s.startsWith("u") && tmpSubject == null) {
					tmpSubject = s;
				}
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.leadingPoint = (leadingPoint == null) ? tmpSubject : leadingPoint;
	}

	public ArrayList<ArrayList<String>> getTriples() {
		ArrayList<ArrayList<String>> ts = new ArrayList<ArrayList<String>>();
		for (Triple t : triples) {
			ts.add(t.getTriple());
		}
		return ts;
	}

	public String getSubject() {
		return leadingPoint;
	}

	public class Triple {
		private String s;
		private String p;
		private String o;

		public Triple(String s, String p, String o) {
			this.s = s;
			this.p = p;
			this.o = o;
		}

		public int hashCode() {
			return (s + p + o).hashCode();
		}

		public ArrayList<String> getTriple() {
			ArrayList<String> t = new ArrayList<String>();
			t.add(s);
			t.add(p);
			t.add(o);
			return t;
		}

		public boolean equals(Object obj) {
			if (obj == null || !(obj instanceof Triple)) {
				return false;
			}
			Triple intriple = (Triple) obj;
			if (intriple.s.equals(s) && intriple.p.equals(p)
					&& intriple.o.equals(o)) {
				return true;
			}
			return false;
		}
	}

	@Override
	public int compareTo(ISentence o) {
		return new SubjectComparator().compare(this.leadingPoint, o.leadingPoint);
	}
}
